
import numpy
import copy
import utl

import matrixTool

unit = 50    #棋格
dotnum = 15  #线条数

#定义数组下标
arrayWidth = 15
arrayHeigh = 15

###棋盘操作类####
class boardOperator():
	def __init__(self): 
		self.unit = unit
		self.dotnum = dotnum
		self.arrayBoard = numpy.zeros((arrayWidth,arrayHeigh),dtype=numpy.int) #定义期盘映射数组的  
		self.matrixTool = matrixTool.matrixTool()   #矩阵计算类

	def getArray(self):  #返回标志数组所有数据
		return self.arrayBoard

	def getValueOfArrayByXY(self , x , y ):  #返回特定数组值
		return self.arrayBoard[x][y]

	#打印数组#
	def dumpArray(self):   #print直接打印原数组是x,y倒翻的
		print(self.getArray())

	#@param 
	# pos 为坐标系 ， 含 x , y 
	# 判断该棋位是否可下棋子
	def isPosCanPlay(self, position): 
	 	pos = self.translatPosToIndex(position)
	 	if(self.arrayBoard[pos.x][pos.y]== 0 ):
	 		return 1
	 	else:
	 		print("棋位不可下，坐标是：",pos)
	 		return 0

	##坐标轴与数组下标是反的 ,位置转下标  ,已将坐标轴与数组下标做了变换
	def translatPosToIndex( self , pos ):
		index = copy.copy(pos)   #浅拷贝

		index.x = (pos.y/unit) - 1
		index.y = (pos.x/unit) - 1

		# print("translatPosToIndex = ",index);
		return index

	# @param 下标转位置函数   , 数组的坐标与坐标的x,y相反的
	def translatIndexToPos(self,index):
		pos = copy.copy(index)
		# pos.x = (index.x + 1) *unit
		# pos.y = (index.y + 1) *unit

		pos.x = (index.y + 1) *unit
		pos.y = (index.x + 1) *unit
		print("pos = ",pos.x , pos.y);
		return pos

	# 设置棋子标志 通过一含x,y的变量设置# self.dumpArray() #未知原因，坐标x,y到反了
	def setBoardFlag( self , index , flag ):
		self.setBoardFlagByXY(index.x ,index.y ,flag )

	# 设置棋子标志 通过一x,y变量设置
	def setBoardFlagByXY( self , x,y , flag ):
		print("setBoardFlagByXY : ",x,y)
		self.arrayBoard[x][y] = flag
	
	# 点击 的位置是否超出棋盘  ,默认没有超出
	def isOutOfChessBoard(self , pos):
		if(pos.x > (self.unit*self.dotnum)):
			return 1
		if(pos.y > (self.unit*self.dotnum)):
			return 1
		if(pos.x <self.unit):
			return 1
		if(pos.y <self.unit):
			return 1
		return 0

    #通过最后一个棋子的下落点，上下，左右，撇捺方向判断是否赢了
	def isWinByIndex(self, flag ,index , array):
		######通过累计标志到达了5（5连1或5连2（flag）),说明赢了
		return (self.isWinX(flag,index,array) or self.isWinY(flag,index,array) 
			or self.isWinLeftOfDirOfDip(flag,index,array)or self.isWinRightOfDirOfDip(flag,index,array))

	##横向，累加到5，则赢
	def isWinX(self, flag ,index , array):  ##横向
		begin_y = index.y - 4   #
		sum  = 0        ##计数标志
		while(begin_y <= index.y + 4):
			if(begin_y < 0 ):
				begin_y = begin_y + 1
				continue
			if (begin_y >= array.shape[1]):
				break
			if(array[index.x][begin_y] != flag):
				sum= 0
			else:
				sum = sum +1

			if(sum == 5):
				print("水平方向判断输赢，成功了")
				return 1   ##连起来了，赢	
			begin_y = begin_y + 1
		return 0


	def isWinY(self, flag ,index , array):  ###纵向
		begin_x = index.x - 4 
		sum = 0 
		while(begin_x <= index.x + 4):
			if(begin_x < 0 ):
				begin_x = begin_x + 1
				continue
			if (begin_x >= array.shape[0]):
				break
			if(array[begin_x][index.y] != flag):
				sum= 0
			else:
				sum = sum +1

			if(sum == 5):
				print("垂直方向判断输赢，成功了")
				return 1   ##连起来了，赢	
			begin_x = begin_x + 1
		return 0   #默认返回0


	##左斜向(撇)判断
	def isWinLeftOfDirOfDip(self, flag ,index , array):   ###左斜向
		#以index 为原点，半径为5的范围(上下，左右加减4)
		#坐标轴与二维数组下标是相反的
		#x,y坐标依次加1判断
		begin_x = index.x - 4
		begin_y = index.y - 4
		sum = 0  ###计数标记
		##循环条件:索引小于等于当前索引x,y加4
		while( begin_x <= index.x + 4 or begin_y <= index.y + 4 ):
			#判断越界情况
			if(begin_x < 0 or begin_y < 0 ):
				begin_x = begin_x + 1
				begin_y = begin_y + 1
				continue
			if(begin_x >= array.shape[0] or begin_y >= array.shape[1]):
				break
			if(array[begin_x][begin_y] != flag):
				sum= 0
			else:
				sum = sum +1

			if(sum == 5):
				print("左斜向判断输赢，成功了")
				return 1   ##连起来了，赢	
			begin_x = begin_x + 1
			begin_y = begin_y + 1
		return 0


	def isWinRightOfDirOfDip(self, flag ,index , array):   ###右斜向
		##x,y依次递减 ， 遍历的坐标
		begin_x = index.x - 4
		begin_y = index.y + 4
		sum = 0 ##计数索引
		while(begin_y >= index.y - 4 or begin_x <= index.x + 4):  ##循环条件随便一个x或y坐标即可
			##数组越界情况考虑
			if(begin_x < 0 or begin_y > array.shape[1] -1 ):
				begin_x = begin_x + 1
				begin_y = begin_y - 1
				continue
			if(begin_x >= array.shape[1] or begin_y < 0 ):
				break

			if(array[begin_x][begin_y] != flag):
				sum= 0
			else:
				sum = sum +1

			if(sum == 5):
				print("右斜向判断输赢，成功了")
				return 1   ##连起来了，赢	
			begin_x = begin_x + 1
			begin_y = begin_y - 1
		return 0

	def getAiIndex(self , flag  , deep ):    ##根据ai计算，返回一个数组下标用于下子
		print("AI下棋.................")
		return self.matrixTool.getAiIndex(self.getArray() , flag , deep) 
		
	def calculateScore(self):
		print("分数统计")






